package com.qa.dom.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qa.dom.dto.HomePageDto;
import com.qa.dom.entity.Usertable;
import com.qa.dom.service.IUsertableService;
import com.qa.dom.util.JsonResult;
import com.qa.dom.util.baseAction;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;

/**
 * 用户管理
 * @author z77z
 * @since 2017-12-25
 */
@Controller
@RequestMapping("/usertable")
    public class UserTableController extends baseAction {

        @Resource
        RedisTemplate redisTemplate;

        @Resource
        private IUsertableService usertableService;

        /*-------------------------------- 我的首页----------------------------------*/
        @RequestMapping("/queryHomePage")
        @ResponseBody
        public JsonResult queryHomePage(Usertable usertable){
            usertable = getCurrentUser();
          return renderSuccess(0,"访问成功", 0,  usertableService.queryHomePage(usertable));
        }

       /*-------------------------------- 我的首页分页----------------------------------*/
        /**
         * 我的老师（主页）
         * 分页
         * @param page
         * @param limit
         * @param usertable
         * @return
         */
        @RequestMapping("/queryHomePageByPage")
        @ResponseBody
        public JsonResult queryHomePageByPage(Integer page,Integer limit){
            Usertable usertable = getCurrentUser();
            Page<HomePageDto> page1 = new Page<>(page, limit);
            Page<HomePageDto> s = usertableService.queryHomePageByPage(page1,usertable);
            return renderSuccess(0,"访问成功", s.getTotal(), s.getRecords());
        }

        /*-------------------------------- 老师详情 ----------------------------------*/
        @RequestMapping("/queryTeacher")
        @ResponseBody
        public JsonResult queryTeacher(Integer  teacherId){
            Usertable usertable = getCurrentUser();
            EntityWrapper<Usertable> wrapper=new EntityWrapper<>();
            wrapper.eq("teacherId",teacherId);
            return renderSuccess(0,"访问成功", 0,  usertableService.queryTeacher(usertable,teacherId));
        }

    /**
     * 修改用户昵称
     * @param id
     * @param nickname
     * @return
     */
    @RequestMapping("/updateStuNickname")
    @ResponseBody
    @Transactional
    public  JsonResult updateStuNickname (Integer id,String nickname){
        return usertableService.updateStuNickname(id,nickname)?renderSuccess(1,"操作成功",0,null):renderError(0,"操作失败",0,null);
    }

    /**
     * 修改头像
     * @param sheadimg
     * @return
     */
    @RequestMapping("/updateStuImg")
    @ResponseBody
    @Transactional
    public JsonResult updateStuImg(String sheadimg){
        Usertable usertable = getCurrentUser();
        return usertableService.updateStuImg(usertable.getId(),sheadimg)?renderSuccess(1,"操作成功",0,null):renderError(0,"操作失败",0,null);
    }


    /**
     * 查询单条用户信息
     * @param id
     * @return
     */
    @RequestMapping("/queryUsertableById")
    @ResponseBody
    public JsonResult queryUsertableById(Integer id){
        return  renderSuccess(1,"",0,usertableService.queryUsertableById(id));
    }

}





